//!
//!       Staggered Mesh for u-vel and v-vel               Notation
//!      
//!       0   1       2       3       4   5             
//!       |   |       |       |       |   |            o Center of volumes    
//!
//!   5-  #---^--->---^--->---^--->---^---#  -4        > u-velocity 
//!       |       |       |       |       |   
//!   4-  >   o   >   o   >   o   >   o   >            ^ v-velocity 
//!       |       |       |       |       |            
//!       ^---^---+---^---+---^---+---^---^  -3        + nodes
//!       |       |       |       |       |         
//!   3-  >   o   >   o   >   o   >   o   >            # corners, here >, ^
//!       |       |       |       |       |              and o are defined
//!       ^---^---+---^---+---^---+---^---^  -2 
//!       |       |       |       |       |            
//!   2-  >   o   >   o   >   o   >   o   >            
//!       |       |       |       |       |            
//!       ^---^---+---^---+---^---+---^---^  -1 
//!       |       |       |       |       |                 
//!   1-  >   o   >   o   >   o   >   o   >                 
//!       |       |       |       |       |                 
//!   0-  #---^--->---^--->---^--->---^---#  -0 
//!
//!      |       |       |       |       |
//!      0       1       2       3       4                  
//!
//!
//!         Indexation of u (>), v (^) and other variables (o).
//!                                                  
//!                      v(i,j) 
//!                  |     n     |               
//!                --+-----^-----+--             
//!                  |           |               
//!                  |           |               
//!     u(i-1,j) = w >     o     > e = u(i,j)
//!                  |   (i,j)   |
//!                  |           |
//!                --+-----^-----+--
//!                  |     s     |
//!                     v(i,j-1)
//!

namespace Tuna {

template<class Tprec, int Dim>
inline bool CDS_CoDi<Tprec, Dim>::calcCoefficients1D() 
{
    prec_t G_dx = Gamma / dx;
    prec_t dx_dt = dx / dt;
    prec_t ce, cw;
    aE = 0.0; aW = 0.0; aP = 0.0; sp = 0.0;

    for (int i =  bi; i <= ei; ++i) 
      {
	ce = u(i ) ;
	cw = u(i-1) ;	    
	aE (i) = G_dx - ce * 0.5 ;
	aW (i) = G_dx + cw * 0.5 ;
	aP (i) = aE (i) + aW (i) + dx_dt;// + (ce - cw);	    
//  The term (ce - cw) is the discretizated continuity equation in 1D 
//  using FVM, and must be equal to zero, so it is possible to 
//  eliminate it from aP coefficient.
	    sp (i) = phi_0(i) * dx_dt ;
	}
    applyBoundaryConditions1D();
    return 0;
}

template<class Tprec, int Dim>
inline bool CDS_CoDi<Tprec, Dim>::calcCoefficients2D() 
{
    prec_t Gdy_dx = Gamma * dy / dx;
    prec_t Gdx_dy = Gamma * dx / dy;
    prec_t dxy_dt = dx * dy / dt;
    prec_t ce, cw, cn, cs;
    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aP = 0.0; 
    sp = 0.0;

    for (int i =  bi; i <= ei; ++i)
	for (int j = bj; j <= ej; ++j)
	{
	    ce = u(i  , j) * dy;
	    cw = u(i-1, j) * dy;
	    cn = v(i, j  ) * dx;
	    cs = v(i, j-1) * dx;
	    
	    aE (i,j) = Gdy_dx - ce * 0.5 ;
	    aW (i,j) = Gdy_dx + cw * 0.5 ;
	    aN (i,j) = Gdx_dy - cn * 0.5;
	    aS (i,j) = Gdx_dy + cs * 0.5;
	    aP (i,j) = aE (i,j) + aW (i,j) + aN (i,j) + aS (i,j) + dxy_dt;
// 		+ (ce - cw) + (cn - cs);	    
//  The term (ce - cw) + (cn - cs) is the discretizated continuity equation 
//  in 2D using FVM, and must be equal to zero, so it is possible to 
//  eliminate it from aP coefficient.
	    sp (i,j) = phi_0(i,j) * dxy_dt ;
	}
    applyBoundaryConditions2D();
    return 0;
}


//!
//!---------------------  3D  ---------------------
//!
template<class Tprec, int Dim>
inline bool CDS_CoDi<Tprec, Dim>::calcCoefficients3D() 
{
    prec_t dyz = dy * dz, dyz_dx = Gamma * dyz / dx;
    prec_t dxz = dx * dz, dxz_dy = Gamma * dxz / dy;
    prec_t dxy = dx * dy, dxy_dz = Gamma * dxy / dz;
    prec_t dxyz_dt = dx * dy * dz / dt;
    prec_t ce, cw, cn, cs, cf, cb;
    aE = 0.0; aW = 0.0; aN = 0.0; aS = 0.0; aF = 0.0; aB = 0.0; aP = 0.0; 
    sp = 0.0;
    
    for (int k = bk; k <= ek; ++k)
	for (int i =  bi; i <= ei; ++i)
	    for (int j = bj; j <= ej; ++j)
	    {
		ce = u(i  , j, k) * dyz;
		cw = u(i-1, j, k) * dyz;
		cn = v(i, j  , k) * dxz;
		cs = v(i, j-1, k) * dxz;
		cf = w(i, j, k  ) * dxy;
		cb = w(i, j, k-1) * dxy;
		
		aE (i,j,k) = dyz_dx - ce * 0.5;
		aW (i,j,k) = dyz_dx + cw * 0.5;
		aN (i,j,k) = dxz_dy - cn * 0.5;
		aS (i,j,k) = dxz_dy + cs * 0.5;
		aF (i,j,k) = dxy_dz - cf * 0.5;
		aB (i,j,k) = dxy_dz + cb * 0.5;
		aP (i,j,k) = aE (i,j,k) + aW (i,j,k)  + aN (i,j,k) + aS (i,j,k)
		    + aF (i,j,k) + aB (i,j,k) + dxyz_dt;
//!		+ (ce - cw) + (cn - cs) + (cf - cb);
//  The term (ce - cw) + (cn - cs) + (cf - cb) is the discretizated 
//  continuity equation in 3D using FVM, and must be equal to zero, so it 
//  is possible to eliminate it from aP coefficient.
		sp (i,j,k) = phi_0(i,j,k) * dxyz_dt ;
	    }
    applyBoundaryConditions3D();   
    return 0;
}
  
} // Tuna namespace














